mruby 4.0.0
mruby is the lightweight implementation of the Ruby language
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1
6
7#ifndef MRUBY_ARRAY_H
8#define MRUBY_ARRAY_H
9
10#include "common.h"
11
12/*
13 * Array class
14 */
16
17typedef struct mrb_shared_array {
18 int refcnt;
19 mrb_ssize len;
20 mrb_value *ptr;
22
23/* On 32-bit platforms whose ABI gives 8-byte members 8-byte alignment
24 (ARM, MIPS, xtensa, ...), an embedded mrb_value array forces 8-byte
25 alignment of the inner union, padding the heap-form layout and
26 inflating struct size past the 5-word RVALUE limit. Disable embedding
27 whenever mrb_value contains an 8-byte aligned member: nan-boxing
28 (uint64_t), or no-boxing with int64_t/double inside the union. */
29#if defined(MRB_32BIT) && \
30 (defined(MRB_NAN_BOXING) || \
31 (defined(MRB_NO_BOXING) && \
32 (!defined(MRB_USE_FLOAT32) || defined(MRB_INT64)))) && \
33 !defined(MRB_ARY_NO_EMBED)
34# define MRB_ARY_NO_EMBED
35#endif
36
37#ifdef MRB_ARY_NO_EMBED
38# define MRB_ARY_EMBED_LEN_MAX 0
39#else
40# define MRB_ARY_EMBED_LEN_MAX ((mrb_int)(sizeof(void*)*3/sizeof(mrb_value)))
41mrb_static_assert(MRB_ARY_EMBED_LEN_MAX > 0, "MRB_ARY_EMBED_LEN_MAX > 0");
42#endif
43
44struct RArray {
45 MRB_OBJECT_HEADER;
46 union {
47 struct {
48 mrb_ssize len;
49 union {
50 mrb_ssize capa;
51 mrb_shared_array *shared;
52 } aux;
53 mrb_value *ptr;
54 } heap;
55#ifndef MRB_ARY_NO_EMBED
56 mrb_value ary[MRB_ARY_EMBED_LEN_MAX];
57#endif
58 } as;
59};
60
61#define mrb_ary_ptr(v) ((struct RArray*)(mrb_ptr(v)))
62#define mrb_ary_value(p) mrb_obj_value((void*)(p))
63#define RARRAY(v) ((struct RArray*)(mrb_ptr(v)))
64
65#ifdef MRB_ARY_NO_EMBED
66#define ARY_EMBED_P(a) 0
67#define ARY_UNSET_EMBED_FLAG(a) (void)0
68#define ARY_EMBED_LEN(a) 0
69#define ARY_SET_EMBED_LEN(a,len) (void)0
70#define ARY_EMBED_PTR(a) ((mrb_value*)NULL)
71#else
72#define MRB_ARY_EMBED_MASK 7
73#define ARY_EMBED_P(a) ((a)->flags & MRB_ARY_EMBED_MASK)
74#define ARY_UNSET_EMBED_FLAG(a) ((a)->flags &= ~(MRB_ARY_EMBED_MASK))
75#define ARY_EMBED_LEN(a) ((mrb_int)(((a)->flags & MRB_ARY_EMBED_MASK) - 1))
76#define ARY_SET_EMBED_LEN(a,len) ((a)->flags = ((a)->flags&~MRB_ARY_EMBED_MASK) | ((uint32_t)(len) + 1))
77#define ARY_EMBED_PTR(a) ((a)->as.ary)
78#endif
79
80#define ARY_LEN(a) (ARY_EMBED_P(a)?ARY_EMBED_LEN(a):(mrb_int)(a)->as.heap.len)
81#define ARY_PTR(a) (ARY_EMBED_P(a)?ARY_EMBED_PTR(a):(a)->as.heap.ptr)
82#define RARRAY_LEN(a) ARY_LEN(RARRAY(a))
83#define RARRAY_PTR(a) ARY_PTR(RARRAY(a))
84#define RARRAY_GETMEM(a, ptr, len) ARY_GETMEM(RARRAY(a), ptr, len)
85#define ARY_SET_LEN(a,n) do {\
86 if (ARY_EMBED_P(a)) {\
87 mrb_assert((n) <= MRB_ARY_EMBED_LEN_MAX); \
88 ARY_SET_EMBED_LEN(a,n);\
89 }\
90 else\
91 (a)->as.heap.len = (n);\
92} while (0)
93#define ARY_CAPA(a) (ARY_EMBED_P(a)?MRB_ARY_EMBED_LEN_MAX:(a)->as.heap.aux.capa)
94#define MRB_ARY_SHARED 256
95#define ARY_SHARED_P(a) ((a)->flags & MRB_ARY_SHARED)
96#define ARY_SET_SHARED_FLAG(a) ((a)->flags |= MRB_ARY_SHARED)
97#define ARY_UNSET_SHARED_FLAG(a) ((a)->flags &= ~MRB_ARY_SHARED)
98#define ARY_GETMEM(a, ptr, len) do { \
99 struct RArray *MRB_UNIQNAME(_a_) = (a); \
100 if (ARY_EMBED_P(MRB_UNIQNAME(_a_))) { \
101 (len) = ARY_EMBED_LEN(MRB_UNIQNAME(_a_)); \
102 (ptr) = ARY_EMBED_PTR(MRB_UNIQNAME(_a_)); \
103 } \
104 else { \
105 (len) = MRB_UNIQNAME(_a_)->as.heap.len; \
106 (ptr) = MRB_UNIQNAME(_a_)->as.heap.ptr; \
107 } \
108} while (0)
109
110MRB_API void mrb_ary_modify(mrb_state*, struct RArray*);
111MRB_API mrb_value mrb_ary_dup(mrb_state*, mrb_value ary);
112MRB_API mrb_value mrb_ary_make_shared_copy(mrb_state*, mrb_value ary);
114
115/*
116 * Initializes a new array.
117 *
118 * Equivalent to:
119 *
120 * Array.new
121 *
122 * @param mrb The mruby state reference.
123 * @return The initialized array.
124 */
126
127/*
128 * Initializes a new array with initial values
129 *
130 * Equivalent to:
131 *
132 * Array[value1, value2, ...]
133 *
134 * @param mrb The mruby state reference.
135 * @param size The number of values.
136 * @param vals The actual values.
137 * @return The initialized array.
138 */
139MRB_API mrb_value mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals);
140
141/*
142 * Initializes a new array with two initial values
143 *
144 * Equivalent to:
145 *
146 * Array[car, cdr]
147 *
148 * @param mrb The mruby state reference.
149 * @param car The first value.
150 * @param cdr The second value.
151 * @return The initialized array.
152 */
154
155/*
156 * Concatenate two arrays. The target array will be modified
157 *
158 * Equivalent to:
159 * ary.concat(other)
160 *
161 * @param mrb The mruby state reference.
162 * @param self The target array.
163 * @param other The array that will be concatenated to self.
164 */
165MRB_API void mrb_ary_concat(mrb_state *mrb, mrb_value self, mrb_value other);
166
167/*
168 * Create an array from the input. It tries calling to_a on the
169 * value. If value does not respond to that, it creates a new
170 * array with just this value.
171 *
172 * @param mrb The mruby state reference.
173 * @param value The value to change into an array.
174 * @return An array representation of value.
175 */
177
178/*
179 * Pushes value into array.
180 *
181 * Equivalent to:
182 *
183 * ary << value
184 *
185 * @param mrb The mruby state reference.
186 * @param ary The array in which the value will be pushed
187 * @param value The value to be pushed into array
188 */
189MRB_API void mrb_ary_push(mrb_state *mrb, mrb_value array, mrb_value value);
190
191/*
192 * Pops the last element from the array.
193 *
194 * Equivalent to:
195 *
196 * ary.pop
197 *
198 * @param mrb The mruby state reference.
199 * @param ary The array from which the value will be popped.
200 * @return The popped value.
201 */
203
204/*
205 * Sets a value on an array at the given index
206 *
207 * Equivalent to:
208 *
209 * ary[n] = val
210 *
211 * @param mrb The mruby state reference.
212 * @param ary The target array.
213 * @param n The array index being referenced.
214 * @param val The value being set.
215 */
216MRB_API void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val);
217
218/*
219 * Replace the array with another array
220 *
221 * Equivalent to:
222 *
223 * ary.replace(other)
224 *
225 * @param mrb The mruby state reference
226 * @param self The target array.
227 * @param other The array to replace it with.
228 */
229MRB_API void mrb_ary_replace(mrb_state *mrb, mrb_value self, mrb_value other);
230
231/*
232 * Unshift an element into the array
233 *
234 * Equivalent to:
235 *
236 * ary.unshift(item)
237 *
238 * @param mrb The mruby state reference.
239 * @param self The target array.
240 * @param item The item to unshift.
241 */
243
244/*
245 * Get nth element in the array
246 *
247 * Equivalent to:
248 *
249 * ary[offset]
250 *
251 * @param ary The target array.
252 * @param offset The element position (negative counts from the tail).
253 */
254MRB_API mrb_value mrb_ary_entry(mrb_value ary, mrb_int offset);
255#define mrb_ary_ref(mrb, ary, n) mrb_ary_entry(ary, n)
256
257/*
258 * Replace subsequence of an array.
259 *
260 * Equivalent to:
261 *
262 * ary[head, len] = rpl
263 *
264 * @param mrb The mruby state reference.
265 * @param self The array from which the value will be partiality replaced.
266 * @param head Beginning position of a replacement subsequence.
267 * @param len Length of a replacement subsequence.
268 * @param rpl The array of replacement elements.
269 * It is possible to pass `mrb_undef_value()` instead of an empty array.
270 * @return The receiver array.
271 */
272MRB_API mrb_value mrb_ary_splice(mrb_state *mrb, mrb_value self, mrb_int head, mrb_int len, mrb_value rpl);
273
274/*
275 * Shifts the first element from the array.
276 *
277 * Equivalent to:
278 *
279 * ary.shift
280 *
281 * @param mrb The mruby state reference.
282 * @param self The array from which the value will be shifted.
283 * @return The shifted value.
284 */
286
287/*
288 * Removes all elements from the array
289 *
290 * Equivalent to:
291 *
292 * ary.clear
293 *
294 * @param mrb The mruby state reference.
295 * @param self The target array.
296 * @return self
297 */
299
300/*
301 * Join the array elements together in a string
302 *
303 * Equivalent to:
304 *
305 * ary.join(sep="")
306 *
307 * @param mrb The mruby state reference.
308 * @param ary The target array
309 * @param sep The separator, can be NULL
310 */
312
313/*
314 * Update the capacity of the array
315 *
316 * @param mrb The mruby state reference.
317 * @param ary The target array.
318 * @param new_len The new capacity of the array
319 */
320MRB_API mrb_value mrb_ary_resize(mrb_state *mrb, mrb_value ary, mrb_int new_len);
321
322
324
325#endif /* MRUBY_ARRAY_H */
void mrb_ary_modify(mrb_state *, struct RArray *)
Prepares an array for modification.
Definition array.c:226
mrb_value mrb_ary_new(mrb_state *mrb)
Creates a new, empty array.
Definition array.c:92
mrb_value mrb_ary_clear(mrb_state *mrb, mrb_value self)
Removes all elements from an array, making it empty.
Definition array.c:1710
mrb_value mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals)
Creates a new array initialized with a given sequence of values.
Definition array.c:143
void mrb_ary_replace(mrb_state *mrb, mrb_value self, mrb_value other)
Replaces the contents of an array with the contents of another array.
Definition array.c:641
mrb_value mrb_ary_new_capa(mrb_state *, mrb_int)
Creates a new array with a specified initial capacity.
Definition array.c:76
mrb_value mrb_ary_shift(mrb_state *mrb, mrb_value self)
Removes and returns the first element from an array.
Definition array.c:886
void mrb_ary_concat(mrb_state *mrb, mrb_value self, mrb_value other)
Concatenates one array to another.
Definition array.c:525
mrb_value mrb_ary_splice(mrb_state *mrb, mrb_value self, mrb_int head, mrb_int len, mrb_value rpl)
Replaces a portion of an array with elements from another array or a single value.
Definition array.c:1160
void mrb_ary_push(mrb_state *mrb, mrb_value array, mrb_value value)
Pushes an element onto the end of an array.
Definition array.c:795
mrb_value mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr)
Creates a new array of size 2, typically used to represent an association (key-value pair).
Definition array.c:161
void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val)
Sets the element at a given index in an array.
Definition array.c:1099
mrb_value mrb_ary_splat(mrb_state *mrb, mrb_value value)
Creates a new array from a given value, performing a "splat" operation.
Definition array.c:1659
mrb_value mrb_ary_join(mrb_state *mrb, mrb_value ary, mrb_value sep)
Joins the elements of an array into a string, separated by a given separator.
Definition array.c:1847
mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary)
Removes and returns the last element from an array.
Definition array.c:861
mrb_value mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item)
Prepends an element to the beginning of an array.
Definition array.c:999
mrb_value mrb_ary_entry(mrb_value ary, mrb_int offset)
Retrieves an element from an array at a specific index.
Definition array.c:1765
mrb_value mrb_ary_resize(mrb_state *mrb, mrb_value ary, mrb_int new_len)
Resizes an array to a new length.
Definition array.c:372
mruby common platform definition"
#define MRB_END_DECL
End declarations in C mode.
Definition common.h:28
#define MRB_BEGIN_DECL
Start declarations in C mode.
Definition common.h:26
#define MRB_API
Declare a public mruby API function.
Definition common.h:108
#define mrb_static_assert(...)
The mrb_static_assert() macro function takes one or two arguments.
Definition mruby.h:108
Definition array.h:44
Definition array.h:17
Definition mruby.h:280
Definition boxing_nan.h:40